package com._21cn.product.service;

import com._21cn.cms.common.Constants;
import com._21cn.framework.base.db.BaseDao;
import com._21cn.framework.base.service.impl.BaseServiceImpl;

import com._21cn.product.dao.*;
import com._21cn.product.entity.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("watchService")
public class WatchService extends BaseServiceImpl<Watch> {
	
    @Autowired
    private WatchDao watchDao;

    @Autowired
    private WatchCateDao watchCateDao;

    @Autowired
    private CateDao cateDao;

    @Autowired
    private WatchPictureDao watchPictureDao;

    @Autowired
    private WatchPropertyDao watchPropertyDao;

    @Autowired
    private PropertyDao propertyDao;

    @Override
    protected BaseDao<Watch> getBaseDao() {
        return watchDao;
    }

    @Override
    public Watch getById( Object id ) {
        return getById( id, true );
    }

    public Watch getById( Object id , boolean extraProperty) {
        Watch watch = super.getById(id);
        if( watch==null ){
            return null;
        }
        if( extraProperty ){
            Map<String, Object> params = new HashMap<String, Object>();
            //手表品牌
            params.put("eqWatchId", id);
            List<WatchCate> cateList = watchCateDao.selectByMap(params, 1, 100);
            for(WatchCate watchCate : cateList){
                if(watchCate.getTypeId().equals(Constants.CATE_TYPE_WATCH_BRAND)){      //品牌
                    Integer cateId = watchCate.getCateId();
                    Cate brand = cateDao.selectById(cateId);
                    watch.setBrand(brand);
                    watch.setBrandId(cateId);
                    watch.setBrandName(brand.getName());
                }
            }

            //手表图片
            params.clear();
            params.put("eqWatchId", id);
            List<WatchPicture> watchPictureList = watchPictureDao.selectByMap(params, 1, 100, false);
            watch.setWatchPictureList(watchPictureList);

            //手表属性
            Map<String, String> carPropertyMap = new HashMap<String, String>();
            params.clear();
            params.put("eqWatchId", id);
            List<WatchProperty> watchPropertyList = watchPropertyDao.selectByMap(params, 1, 500, false);
            for(WatchProperty watchProperty : watchPropertyList){
                Integer propertyId = watchProperty.getPropertyId();
                Property property = propertyDao.selectById(propertyId);
                if( property!=null ){
                    watchProperty.setDefaultValue(property.getDefaultValue());
                    watchProperty.setSortNum(property.getSortNum());
                    watchProperty.setPropertyName(property.getName());
                    String code = property.getCode();
                    carPropertyMap.put(code, watchProperty.getPropertyValue());
                }
            }
            watch.setWatchPropertyList(watchPropertyList);
            watch.setWatchPropertyMap(carPropertyMap);
        }
        return watch;
    }

}